import { defineStore } from 'pinia'
import { getUserInfo } from '@/api/user'

export const useUserStore = defineStore('user', {
  state: () => ({
    userInfo: null,
    token: localStorage.getItem('token') || null
  }),
  actions: {
    setToken(newToken) {
      this.token = newToken
      localStorage.setItem('token', newToken)
    },
    getToken() {
      return localStorage.getItem('token')
    },

    setUserInfo(userData) {
      this.userInfo = userData
      localStorage.setItem('userInfo', JSON.stringify(userData)) 
    },
    clearUserInfo() {
      this.userInfo = null
      localStorage.removeItem('userInfo')
    },
    getUserInfo2()
    {
       return JSON.parse(localStorage.getItem('userInfo'))
    },
    getUser()
    {
      return this.userInfo
    },
    async getUserInfo() {
      const { data } = await getUserInfo()
      this.userInfo = data.data
      return data.data
    },
    clearToken() {
      this.token = null
      localStorage.removeItem('token') 
    },
  }
})